package com.bjh.controller;

import com.bjh.service.WalletService;
import com.bjh.util.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/agent/wallet")
public class WalletController extends BaseController {
    @Autowired
    private WalletService walletService;

    /**
     * 获取代理商保证金
     * @param agentId 代理商id
     * @return {@link Map}
     */
    @GetMapping("/getBond")
    public Map<String, Object> getBond (@RequestParam Long agentId){
        logBefore(log, "++++++++++ 获取代理商保证金 ++++++++++");
        try{
            Map<String, Object> resMap = new HashMap<>(walletService.getBond(agentId));
            logAfter(log);
            return returnSuccessWithData(resMap);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return returnResult(1, "获取失败");
        }
    }

    /**
     * 获取代理商保证金记录
     * @param agentId 代理商id
     * @param pageIndex 起始页
     * @param pageSize 分页大小
     * @return {@link Map}
     */
    @GetMapping("/getBondLog")
    public Map<String, Object> getBondLog (@RequestParam Long agentId, @RequestParam(required = false) String bondRange,
    @RequestParam Long pageIndex, @RequestParam Long pageSize){
        logBefore(log, "++++++++++ 获取代理商保证金记录 ++++++++++");
        try{
            Map<String, Object> params = new HashMap<>();
            params.put("agentId", agentId);
            if (!StringUtils.isEmpty(bondRange)){
                String[] timeRange = bondRange.split(" ~ ");
                params.put("minRange", timeRange[0] + " 00:00:00");
                params.put("maxRange", timeRange[1] + " 23:59:59");
            }
            params.put("start", (pageIndex - 1) * pageSize);
            params.put("length", pageSize);
            Map<String, Object> resMap = new HashMap<>(walletService.getBondLog(params));
            logAfter(log);
            return returnSuccessWithData(resMap);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return returnResult(1, "获取失败");
        }
    }

    /**
     * 获取代理商钱包
     * @param agentId 代理商id
     * @return {@link Map}
     */
    @GetMapping("/getWallet")
    public Map<String, Object> getWallet (@RequestParam Long agentId){
        logBefore(log, "++++++++++ 获取代理商钱包 ++++++++++");
        try{
            Map<String, Object> resMap = new HashMap<>(walletService.getWallet(agentId));
            logAfter(log);
            return returnSuccessWithData(resMap);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return returnResult(1, "获取失败");
        }
    }

    /**
     * 获取代理商钱包明细记录
     * @param agentId 代理商id
     * @param pageIndex 起始页
     * @param pageSize 分页大小
     * @return {@link Map}
     */
    @GetMapping("/getWalletLog")
    public Map<String, Object> getWalletLog (@RequestParam Long agentId, @RequestParam(required = false) String walletRange,
        @RequestParam Long pageIndex, @RequestParam Long pageSize){
        logBefore(log, "++++++++++ 获取代理商钱包明细记录 ++++++++++");
        try{
            Map<String, Object> params = new HashMap<>();
            params.put("agentId", agentId);
            if (!StringUtils.isEmpty(walletRange)){
                System.out.println(walletRange);
                String[] timeRange = walletRange.split(" ~ ");
                params.put("minRange", timeRange[0] + " 00:00:00");
                params.put("maxRange", timeRange[1] + " 23:59:59");
            }
            params.put("start", (pageIndex - 1) * pageSize);
            params.put("length", pageSize);
            Map<String, Object> resMap = new HashMap<>(walletService.getWalletLog(params));
            logAfter(log);
            return returnSuccessWithData(resMap);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return returnResult(1, "获取失败");
        }
    }

    /**
     * 获取优惠券列表
     * @param agentId 代理商id
     * @param pageIndex 起始页
     * @param pageSize 分页大小
     * @return {@link Map}
     */
    @GetMapping("/getCouponList")
    public Map<String, Object> getCouponList (@RequestParam Long agentId, @RequestParam(required = false) Long status,
                                             @RequestParam Long pageIndex, @RequestParam Long pageSize){
        logBefore(log, "++++++++++ 获取优惠券列表 ++++++++++");
        try{
            Map<String, Object> params = new HashMap<>();
            params.put("agentId", agentId);
            if(!StringUtils.isEmpty(status)) params.put("status", status);
            params.put("start", (pageIndex - 1) * pageSize);
            params.put("length", pageSize);
            Map<String, Object> resMap = new HashMap<>(walletService.getCouponList(params));
            logAfter(log);
            return returnSuccessWithData(resMap);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return returnResult(1, "获取失败");
        }
    }
}
